﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace CoderJoe.WoW.Services
{
	public class ItemService
	{
		private List<Item> ItemCache { get; set; }
        private List<ItemDetail> ItemDetailCache { get; set; }
        private List<string> InventoryTypes { get; set; }
        private List<string> Bondings { get; set; }

		public ItemService()
		{
			ItemCache = new List<Item>();
            ItemDetailCache = new List<ItemDetail>();
            InventoryTypes = new List<string>();

            InventoryTypes.Add(String.Empty);//0
            InventoryTypes.Add("Head");      //1
            InventoryTypes.Add("Neck");      //2
            InventoryTypes.Add("Shoulder");  //3
            InventoryTypes.Add("Shirt");     //4
            InventoryTypes.Add("Shirt");     //5
            InventoryTypes.Add("Belt");      //6
            InventoryTypes.Add("Pants");     //7
            InventoryTypes.Add("Boots");     //8
            InventoryTypes.Add("Bracer");    //9
            InventoryTypes.Add("Gloves");    //10
            InventoryTypes.Add("Ring");      //11
            InventoryTypes.Add("Trinket");   //12
            InventoryTypes.Add("13");        //13
            InventoryTypes.Add("14");        //14
            InventoryTypes.Add("15");        //15
            InventoryTypes.Add("Back");      //16
            InventoryTypes.Add("17");        //17
            InventoryTypes.Add("18");        //18
            InventoryTypes.Add("Tabard");    //19
            InventoryTypes.Add("Robe");      //20
            InventoryTypes.Add("Main Hand"); //21
            InventoryTypes.Add("24");        //22
            InventoryTypes.Add("Held in Off-hand");      //23
            InventoryTypes.Add("24");        //24
            InventoryTypes.Add("25");        //25
            InventoryTypes.Add("Ranged");    //26

            Bondings = new List<string>();
            Bondings.Add(String.Empty);
            Bondings.Add("Binds when picked up");   // 1
            Bondings.Add("Binds when equipped");   // 2
		}

        public string TranslateInventoryTypeIdToName(int slotId)
        {
            if (slotId < 0 || slotId >= InventoryTypes.Count)
                return slotId.ToString();

            return InventoryTypes[slotId];
        }

        public string TranslateBondingToName(int bonding)
        {
            if (bonding < 0 || bonding >= Bondings.Count)
                return "unknown " + bonding.ToString();

            return Bondings[bonding];
        }

		public Bitmap GetItemImage(string imageName)
		{
			CoderJoe.WoW.Proxies.Armory.WoWArmoryProxy proxy = new CoderJoe.WoW.Proxies.Armory.WoWArmoryProxy();
			return proxy.GetItemImage(imageName);
		}

		public Item FindItemByItemId(int itemId)
		{
			return
				(from i in ItemCache
				 where i.ItemId == itemId
				 select i).FirstOrDefault();
		}

		public Item FindOrLoadItem(int itemId)
		{
			Item result = FindItemByItemId(itemId);

			if (result == null)
				result = RefreshItem(itemId);

			return result;
		}

		public Item RefreshItem(int itemId)
		{
			Item existing = FindItemByItemId(itemId);
			if (existing != null)
			{
				ItemCache.Remove(existing);
			}

			CoderJoe.WoW.Proxies.Armory.WoWArmoryProxy proxy = new CoderJoe.WoW.Proxies.Armory.WoWArmoryProxy();
			Item remoteItem = proxy.GetItem(itemId);
			ItemCache.Add(remoteItem);
			return remoteItem;
		}

        public ItemDetail FindItemDetailByItemDetailId(int itemId)
        {
            return
                (from i in ItemDetailCache
                 where i.ItemId == itemId
                 select i).FirstOrDefault();
        }

        public ItemDetail FindOrLoadItemDetail(int itemId)
        {
            ItemDetail result = FindItemDetailByItemDetailId(itemId);

            if (result == null)
                result = RefreshItemDetail(itemId);

            return result;
        }

        public ItemDetail RefreshItemDetail(int itemId)
        {
            ItemDetail existing = FindItemDetailByItemDetailId(itemId);
            if (existing != null)
            {
                ItemDetailCache.Remove(existing);
            }

            CoderJoe.WoW.Proxies.Armory.WoWArmoryProxy proxy = new CoderJoe.WoW.Proxies.Armory.WoWArmoryProxy();
            ItemDetail remoteItemDetail = proxy.GetItemDetail(itemId);
            ItemDetailCache.Add(remoteItemDetail);
            return remoteItemDetail;
        }
	}
}
